package com.elinkway.infinitemovies.utils;

import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.telephony.TelephonyManager;

import com.elinkway.infinitemovies.application.MoviesApplication;
import com.elinkway.infinitemovies.ui.activity.play.PlayerUtils;
import com.le123.ysdq.R;

public class NetWorkTypeUtils {

    public static final int NETTYPE_NO = 0;
    public static final int NETTYPE_WIFI = 1;
    public static final int NETTYPE_2G = 2;
    public static final int NETTYPE_3G = 3;
    public static final int NETTYPE_4G = 4;

    public final static int TYPE_WIFI = 1;
    public final static int TYPE_MOBILE = 2;
    public final static int TYPE_ERROR = -1;

    public static final String NET_WORK_INVAILABLE = "netInvailable";
    public final static String CMWAP = "cmwap";
    public final static String WAP_3G = "3gwap";
    public final static String UNIWAP = "uniwap";
    public final static String CTWAP = "ctwap";
    public final static String CTNET = "ctnet";
    public static final String CODE_HTTP_SUCCEED = "200";//Networking success
    public static final String CODE_HTTPS_RECONNECT = "002";
    public static final String CODE_SESSION_EXPIRED = "2000";//Session expires
    public static final String CODE_STOP_SERVER = "5000";//Server to stop the service
    public static final String SERVER_NOT_RESPONDING = "10000";//Server did not respond
    public static final String CODE_PAGE_NOT_FOUND = "404";//Did not find interface
    public static final String SERVER_ERROR = "500";
    public static final String CODE_CONNECTION_EXCEPTION = "502";
    public static final String CODE_HTTP_RESTART_CLIENT = "4000";//Restart the client
    public static final String CODE_ERROR_RIGHT = "0";

    public static boolean isNetAvailable() {
        boolean isAvailable = false;
        NetworkInfo info = getAvailableNetWorkInfo();
        if (info != null && info.isAvailable()) {
            isAvailable = true;
        }
        return isAvailable;
    }

    public static boolean isThirdGeneration() {
        TelephonyManager telephonyManager = (TelephonyManager) MoviesApplication
                .getInstance().getSystemService(Context.TELEPHONY_SERVICE);
        int netWorkType = telephonyManager.getNetworkType();
        switch (netWorkType) {
            case TelephonyManager.NETWORK_TYPE_GPRS:
            case TelephonyManager.NETWORK_TYPE_CDMA:
            case TelephonyManager.NETWORK_TYPE_EDGE:

                return false;
            default:
                return true;
        }
    }

    public static boolean isWifi() {

        NetworkInfo networkInfo = getAvailableNetWorkInfo();

        if (networkInfo != null) {

            if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
                return true;
            }

        }

        return false;
    }

    public static NetworkInfo getAvailableNetWorkInfo() {
        NetworkInfo activeNetInfo = null;
        try {
            ConnectivityManager connectivityManager = (ConnectivityManager) MoviesApplication
                    .getInstance().getSystemService(
                            Context.CONNECTIVITY_SERVICE);
            activeNetInfo = connectivityManager.getActiveNetworkInfo();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        if (activeNetInfo != null && activeNetInfo.isAvailable()) {
            return activeNetInfo;
        } else {
            return null;
        }
    }

    public static String getNetWorkType() {

        String netWorkType = "";
        NetworkInfo netWorkInfo = getAvailableNetWorkInfo();

        if (netWorkInfo != null) {
            if (netWorkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
                netWorkType = "1";
            } else if (netWorkInfo.getType() == ConnectivityManager.TYPE_MOBILE) {

                TelephonyManager telephonyManager = (TelephonyManager) MoviesApplication
                        .getInstance().getSystemService(
                                Context.TELEPHONY_SERVICE);

                switch (telephonyManager.getNetworkType()) {
                    case TelephonyManager.NETWORK_TYPE_GPRS:
                        netWorkType = "2";
                        break;
                    case TelephonyManager.NETWORK_TYPE_EDGE:
                        netWorkType = "3";
                        break;
                    case TelephonyManager.NETWORK_TYPE_UMTS:
                        netWorkType = "4";
                        break;
                    // case TelephonyManager.NETWORK_TYPE_HSDPA:
                    // netWorkType = "5";
                    // break;
                    // case TelephonyManager.NETWORK_TYPE_HSUPA:
                    // netWorkType = "6";
                    // break;
                    // case TelephonyManager.NETWORK_TYPE_HSPA:
                    // netWorkType = "7";
                    // break;
                    case TelephonyManager.NETWORK_TYPE_CDMA:
                        netWorkType = "8";
                        break;
                    case TelephonyManager.NETWORK_TYPE_EVDO_0:
                        netWorkType = "9";
                        break;
                    case TelephonyManager.NETWORK_TYPE_EVDO_A:
                        netWorkType = "10";
                        break;
                    case TelephonyManager.NETWORK_TYPE_1xRTT:
                        netWorkType = "11";
                    default:
                        netWorkType = "-1";
                }

            }

        }
        return netWorkType;
    }

    /**
     * 0 无网络 1 WIFI 2 2G 3 3G
     * @return
     */
    public static int getNetType() {

        ConnectivityManager connectivityManager = (ConnectivityManager) MoviesApplication
                .getInstance().getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();

        if (networkInfo != null && networkInfo.isAvailable()) {
            if (ConnectivityManager.TYPE_WIFI == networkInfo.getType()) {
                return NETTYPE_WIFI;
            } else {
                TelephonyManager telephonyManager = (TelephonyManager) MoviesApplication
                        .getInstance().getSystemService(
                                Context.TELEPHONY_SERVICE);

                switch (telephonyManager.getNetworkType()) {
                    case TelephonyManager.NETWORK_TYPE_GPRS:
                    case TelephonyManager.NETWORK_TYPE_CDMA:
                    case TelephonyManager.NETWORK_TYPE_EDGE:
                        return NETTYPE_2G;
                    case TelephonyManager.NETWORK_TYPE_LTE:
                        return NETTYPE_4G;
                    default:
                        return NETTYPE_3G;
                }
            }
        } else {
            return NETTYPE_NO;
        }
    }

    public static boolean isNetworkAvailable(Context context) {
        boolean isAvailable = false;
        if (null != context) {
            NetworkInfo info = getNetworkInfo(context);
            if (info != null && info.isAvailable()) {
                isAvailable = true;
            }
        }
        return isAvailable;
    }

    public static NetworkInfo getNetworkInfo(Context context) {
        try {
            final ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            if (null != connectivityManager) {
                return connectivityManager.getActiveNetworkInfo();
            }
        } catch (Exception e) {

        }
        return null;
    }

    public static int reportNetType(Context context) {
        int netMode = TYPE_ERROR;

        try {
            NetworkInfo info = getNetworkInfo(context);
            if (info != null && info.isAvailable()) {
                int netType = info.getType();
                if (netType == ConnectivityManager.TYPE_WIFI) {
                    netMode = TYPE_WIFI;
                } else if (netType == ConnectivityManager.TYPE_MOBILE) {
                    netMode = TYPE_MOBILE;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return netMode;
    }


    /*
     * 判读网络类别并作出相应提示并返回相应网络类型用
     * 0 无网 1 2g/3g 2 wifi 4无效
     *
     * qinguoli
     *
     * */
    public static int checkNet(Intent intent, int priorNetType) {
        //初始化为4 无效
        int nowNetType = 4;
        String action = intent.getAction();
        if (null == action || "".equals(action))
            nowNetType = 4;
        if (action.equals(PlayerUtils.CONNECTIVTY_CHANGE)) {
            int netType = NetWorkTypeUtils.getNetType();
            LogUtils.e("", "---------------netType-------------" + netType);
            if (NetWorkTypeUtils.NETTYPE_NO == netType) {
                if (0 != priorNetType) {
                    ToastUtil.showShortToast(MoviesApplication.getInstance(), R.string.nonet_tip);
                }
                nowNetType = 0;
            } else if (NetWorkTypeUtils.NETTYPE_2G == netType || NetWorkTypeUtils.NETTYPE_3G == netType) {
                if (1 != priorNetType) {
                    ToastUtil.showShortToast(MoviesApplication.getInstance(), R.string.moblie_net_tip_new);
                }
                nowNetType = 1;
            } else if (NetWorkTypeUtils.NETTYPE_WIFI == netType) {
                if (2 != priorNetType) {
                    ToastUtil.showShortToast(MoviesApplication.getInstance(), R.string.wifi_tip);
                }
                nowNetType = 2;
            }
        }
        return nowNetType;
    }

    public static String getNetMode(Context context) {
        String netMode = "";
        try {
            NetworkInfo info = getNetworkInfo(context);
            if (info == null || !info.isAvailable()) {
                netMode = NET_WORK_INVAILABLE;
            } else {
                int netType = info.getType();
                if (netType == ConnectivityManager.TYPE_WIFI) {
                    netMode = info.getTypeName();
                } else if (netType == ConnectivityManager.TYPE_MOBILE) {
                    netMode = info.getExtraInfo();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if ("epc.tmobile.com".equals(netMode) || "".equals(netMode)) {
                netMode = "3G";
            }
        }
        return netMode;
    }
}
